package com.syezon.wifikey.news;

import android.animation.ValueAnimator;
import android.os.Bundle;
import android.os.Handler;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.blankj.utilcode.util.SizeUtils;
import com.syezon.wifikey.R;
import com.syezon.wifikey.base.BaseFragment;
import com.syezon.wifikey.safe.SafeCheckActivity;
import com.syezon.wifikey.speed.SpeedTestActivity;
import com.syezon.wifikey.utils.WifiUtil;

import butterknife.BindView;
import butterknife.OnClick;

public class WifiEnhanceResultFragment extends BaseFragment {

    @BindView(R.id.rl_top)
    RelativeLayout mRlTop;
    @BindView(R.id.rl_bottom)
    RelativeLayout mRlBtom;

    private static final String ARG_SSID = "ssid";
    @BindView(R.id.tv_ssid_enhance)
    TextView mTvSsidEnhance;

    private Handler mHandler = new Handler();

    public WifiEnhanceResultFragment() {
    }


    public static WifiEnhanceResultFragment newInstance(String ssid) {
        WifiEnhanceResultFragment fragment = new WifiEnhanceResultFragment();
        Bundle args = new Bundle();
        args.putString(ARG_SSID, ssid);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    protected int getContentViewLayoutID() {
        return R.layout.app_news_layout_wifi_enhance_result;
    }

    @Override
    protected void initViewsAndEvents() {
        Bundle arguments = getArguments();
        String ssid = "";
        if (arguments != null) {
            ssid = arguments.getString(ARG_SSID, "");
        }
        mTvSsidEnhance.setText(ssid + " 增强完成");
        mHandler.postDelayed(new Runnable() {
            @Override
            public void run() {
                ValueAnimator valueAnimator = ValueAnimator.ofInt(SizeUtils.dp2px(50), SizeUtils.dp2px(10)).setDuration(500);
                valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                    @Override
                    public void onAnimationUpdate(ValueAnimator animation) {
                        int marginTop = (int) animation.getAnimatedValue();
                        ViewGroup.MarginLayoutParams marginLayoutParams = (ViewGroup.MarginLayoutParams) mRlTop.getLayoutParams();
                        marginLayoutParams.topMargin = marginTop;
                        mRlTop.setLayoutParams(marginLayoutParams);
                    }
                });
                valueAnimator.start();
                ValueAnimator valueAnimator1 = ValueAnimator.ofInt(SizeUtils.dp2px(70), SizeUtils.dp2px(13)).setDuration(500);
                valueAnimator1.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                    @Override
                    public void onAnimationUpdate(ValueAnimator animation) {
                        int marginTop = (int) animation.getAnimatedValue();
                        ViewGroup.MarginLayoutParams marginLayoutParams = (ViewGroup.MarginLayoutParams) mRlBtom.getLayoutParams();
                        marginLayoutParams.topMargin = marginTop;
                        mRlBtom.setLayoutParams(marginLayoutParams);
                    }
                });
                valueAnimator1.start();
            }
        }, 1000);
    }

    @OnClick({R.id.tv_speedtest})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_speedtest:
                String wifiSSID = WifiUtil.getWifiSSID();
                if (!TextUtils.isEmpty(wifiSSID)) {
                    Bundle speedTestBundle = new Bundle();
                    speedTestBundle.putString(SpeedTestActivity.ARG_SSID, wifiSSID);
                    readyGo(SpeedTestActivity.class, speedTestBundle);
                }
                break;
        }
    }

}
